# -*- coding: utf-8 -*-
"""
@Time ： 2023/5/24 15:08
@Auth ： 刘佳
@File ：signupform.py
@IDE ：PyCharm
@Motto:MyAI
"""
from django import forms
from django.contrib.auth import authenticate
from django.core import validators

from lucky.models import UserInfo


class signupform(forms.Form):
    username = forms.CharField(label="用户账号", min_length=6, max_length=20,
                               widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "请输入用户账号"}),
                               error_messages={
                                   "required": "用户账号不能为空",
                                   "min_length": "用户名最小长度为6位",
                               },
                               )
    realname = forms.CharField(label="用户昵称", min_length=1, max_length=10)
    password = forms.CharField(label="密码", min_length=6, max_length=20,
                               error_messages={
                                    "min_length": "密码最小长度为6位",
                                    "required": "密码不能为空",
                               },
                               widget=forms.PasswordInput(attrs={"class": "form-control", "placeholder": "请输入密码"}),
                               )
    confirm_pwd = forms.CharField(label="确认密码", min_length=6, max_length=20,
                                  error_messages={
                                      "min_length": "密码最小长度为6位",
                                      "required": "密码不能为空",
                                  },
                                  widget=forms.PasswordInput(attrs={"class": "form-control", "placeholder": "请输入密码"}), )
    phone = forms.CharField(validators=[validators.RegexValidator("1[345678]\d{9}", message='请输入正确格式的手机号码！')])
    email = forms.EmailField(label="邮箱",
                             error_messages={
                                 "required": "邮箱不能为空",
                             },
                             widget=forms.EmailInput(attrs={"class": "form-control", "placeholder": "请输入邮箱"})
                             )

    def clean(self):
        # 以后遇到表单验证中  判断两个字段是否相等
        # 等待验证成功以后再判断这个时候需要重写clean方法  继承于父clean方法
        clean_data = super().clean()

        username = self.cleaned_data.get("username", "")
        password = self.cleaned_data.get("password", "")
        user = authenticate(username=username, password=password)
        if not user:
            raise forms.ValidationError("用户名或密码错误")

        self.cleaned_data["user"] = user

        password = self.cleaned_data.get('password')
        repassword = self.cleaned_data.get('confirm_pwd')
        if password != repassword:
            raise forms.ValidationError(message="两次密码不一致")
        return self.cleaned_data

    def clean_telephone(self):  # 如果你想进一步对字段进行验证  系统自动调用clean_
        phone = self.cleaned_data.get('phone')
        exists = UserInfo.objects.filter(phone=phone).exists()
        if exists:
            raise forms.ValidationError("手机号码已经存在！")
        return phone

    def clean_email(self):
        email = self.cleaned_data["email"]
        if email and UserInfo.objects.filter(email=email).exists():
            raise forms.ValidationError("邮箱已存在")
        return email
